<?php

require_once 'propel/om/BaseObject.php';

require_once 'propel/om/Persistent.php';


include_once 'propel/util/Criteria.php';

include_once 'model/CompanyPeer.php';

/**
 * Base class that represents a row from the 'company' table.
 *
 * 
 *
 * @package model.om
 */
abstract class BaseCompany extends BaseObject  implements Persistent {


	/**
	 * The Peer class.
	 * Instance provides a convenient way of calling static methods on a class
	 * that calling code may not be able to identify.
	 * @var CompanyPeer
	 */
	protected static $peer;


	/**
	 * The value for the id field.
	 * @var int
	 */
	protected $id;


	/**
	 * The value for the name field.
	 * @var string
	 */
	protected $name;


	/**
	 * The value for the address1 field.
	 * @var string
	 */
	protected $address1;


	/**
	 * The value for the address2 field.
	 * @var string
	 */
	protected $address2;


	/**
	 * The value for the zip_code field.
	 * @var string
	 */
	protected $zip_code;


	/**
	 * The value for the city field.
	 * @var string
	 */
	protected $city;


	/**
	 * The value for the country field.
	 * @var string
	 */
	protected $country;


	/**
	 * The value for the phone field.
	 * @var string
	 */
	protected $phone;


	/**
	 * The value for the fax field.
	 * @var string
	 */
	protected $fax;


	/**
	 * The value for the url field.
	 * @var string
	 */
	protected $url;


	/**
	 * The value for the email field.
	 * @var string
	 */
	protected $email;


	/**
	 * The value for the comments field.
	 * @var string
	 */
	protected $comments;


	/**
	 * The value for the logo field.
	 * @var string
	 */
	protected $logo;


	/**
	 * The value for the created_on field.
	 * @var int
	 */
	protected $created_on;


	/**
	 * The value for the updated_on field.
	 * @var int
	 */
	protected $updated_on;

	/**
	 * Collection to store aggregation of collUsers.
	 * @var array
	 */
	protected $collUsers;

	/**
	 * The criteria used to select the current contents of collUsers.
	 * @var Criteria
	 */
	protected $lastUserCriteria = null;

	/**
	 * Collection to store aggregation of collProjects.
	 * @var array
	 */
	protected $collProjects;

	/**
	 * The criteria used to select the current contents of collProjects.
	 * @var Criteria
	 */
	protected $lastProjectCriteria = null;

	/**
	 * Collection to store aggregation of collWorkorders.
	 * @var array
	 */
	protected $collWorkorders;

	/**
	 * The criteria used to select the current contents of collWorkorders.
	 * @var Criteria
	 */
	protected $lastWorkorderCriteria = null;

	/**
	 * Flag to prevent endless save loop, if this object is referenced
	 * by another object which falls in this transaction.
	 * @var boolean
	 */
	protected $alreadyInSave = false;

	/**
	 * Flag to prevent endless validation loop, if this object is referenced
	 * by another object which falls in this transaction.
	 * @var boolean
	 */
	protected $alreadyInValidation = false;

	/**
	 * Get the [id] column value.
	 * 
	 * @return int
	 */
	public function getId()
	{

		return $this->id;
	}

	/**
	 * Get the [name] column value.
	 * 
	 * @return string
	 */
	public function getName()
	{

		return $this->name;
	}

	/**
	 * Get the [address1] column value.
	 * 
	 * @return string
	 */
	public function getAddress1()
	{

		return $this->address1;
	}

	/**
	 * Get the [address2] column value.
	 * 
	 * @return string
	 */
	public function getAddress2()
	{

		return $this->address2;
	}

	/**
	 * Get the [zip_code] column value.
	 * 
	 * @return string
	 */
	public function getZipCode()
	{

		return $this->zip_code;
	}

	/**
	 * Get the [city] column value.
	 * 
	 * @return string
	 */
	public function getCity()
	{

		return $this->city;
	}

	/**
	 * Get the [country] column value.
	 * 
	 * @return string
	 */
	public function getCountry()
	{

		return $this->country;
	}

	/**
	 * Get the [phone] column value.
	 * 
	 * @return string
	 */
	public function getPhone()
	{

		return $this->phone;
	}

	/**
	 * Get the [fax] column value.
	 * 
	 * @return string
	 */
	public function getFax()
	{

		return $this->fax;
	}

	/**
	 * Get the [url] column value.
	 * 
	 * @return string
	 */
	public function getUrl()
	{

		return $this->url;
	}

	/**
	 * Get the [email] column value.
	 * 
	 * @return string
	 */
	public function getEmail()
	{

		return $this->email;
	}

	/**
	 * Get the [comments] column value.
	 * 
	 * @return string
	 */
	public function getComments()
	{

		return $this->comments;
	}

	/**
	 * Get the [logo] column value.
	 * 
	 * @return string
	 */
	public function getLogo()
	{

		return $this->logo;
	}

	/**
	 * Get the [optionally formatted] [created_on] column value.
	 * 
	 * @param string $format The date/time format string (either date()-style or strftime()-style).
	 *							If format is NULL, then the integer unix timestamp will be returned.
	 * @return mixed Formatted date/time value as string or integer unix timestamp (if format is NULL).
	 * @throws PropelException - if unable to convert the date/time to timestamp.
	 */
	public function getCreatedOn($format = 'Y-m-d H:i:s')
	{

		if ($this->created_on === null || $this->created_on === '') {
			return null;
		} elseif (!is_int($this->created_on)) {
			// a non-timestamp value was set externally, so we convert it
			$ts = strtotime($this->created_on);
			if ($ts === -1 || $ts === false) { // in PHP 5.1 return value changes to FALSE
				throw new PropelException("Unable to parse value of [created_on] as date/time value: " . var_export($this->created_on, true));
			}
		} else {
			$ts = $this->created_on;
		}
		if ($format === null) {
			return $ts;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $ts);
		} else {
			return date($format, $ts);
		}
	}

	/**
	 * Get the [optionally formatted] [updated_on] column value.
	 * 
	 * @param string $format The date/time format string (either date()-style or strftime()-style).
	 *							If format is NULL, then the integer unix timestamp will be returned.
	 * @return mixed Formatted date/time value as string or integer unix timestamp (if format is NULL).
	 * @throws PropelException - if unable to convert the date/time to timestamp.
	 */
	public function getUpdatedOn($format = 'Y-m-d H:i:s')
	{

		if ($this->updated_on === null || $this->updated_on === '') {
			return null;
		} elseif (!is_int($this->updated_on)) {
			// a non-timestamp value was set externally, so we convert it
			$ts = strtotime($this->updated_on);
			if ($ts === -1 || $ts === false) { // in PHP 5.1 return value changes to FALSE
				throw new PropelException("Unable to parse value of [updated_on] as date/time value: " . var_export($this->updated_on, true));
			}
		} else {
			$ts = $this->updated_on;
		}
		if ($format === null) {
			return $ts;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $ts);
		} else {
			return date($format, $ts);
		}
	}

	/**
	 * Set the value of [id] column.
	 * 
	 * @param int $v new value
	 * @return void
	 */
	public function setId($v)
	{

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = CompanyPeer::ID;
		}

	} // setId()

	/**
	 * Set the value of [name] column.
	 * 
	 * @param string $v new value
	 * @return void
	 */
	public function setName($v)
	{

		if ($this->name !== $v) {
			$this->name = $v;
			$this->modifiedColumns[] = CompanyPeer::NAME;
		}

	} // setName()

	/**
	 * Set the value of [address1] column.
	 * 
	 * @param string $v new value
	 * @return void
	 */
	public function setAddress1($v)
	{

		if ($this->address1 !== $v) {
			$this->address1 = $v;
			$this->modifiedColumns[] = CompanyPeer::ADDRESS1;
		}

	} // setAddress1()

	/**
	 * Set the value of [address2] column.
	 * 
	 * @param string $v new value
	 * @return void
	 */
	public function setAddress2($v)
	{

		if ($this->address2 !== $v) {
			$this->address2 = $v;
			$this->modifiedColumns[] = CompanyPeer::ADDRESS2;
		}

	} // setAddress2()

	/**
	 * Set the value of [zip_code] column.
	 * 
	 * @param string $v new value
	 * @return void
	 */
	public function setZipCode($v)
	{

		if ($this->zip_code !== $v) {
			$this->zip_code = $v;
			$this->modifiedColumns[] = CompanyPeer::ZIP_CODE;
		}

	} // setZipCode()

	/**
	 * Set the value of [city] column.
	 * 
	 * @param string $v new value
	 * @return void
	 */
	public function setCity($v)
	{

		if ($this->city !== $v) {
			$this->city = $v;
			$this->modifiedColumns[] = CompanyPeer::CITY;
		}

	} // setCity()

	/**
	 * Set the value of [country] column.
	 * 
	 * @param string $v new value
	 * @return void
	 */
	public function setCountry($v)
	{

		if ($this->country !== $v) {
			$this->country = $v;
			$this->modifiedColumns[] = CompanyPeer::COUNTRY;
		}

	} // setCountry()

	/**
	 * Set the value of [phone] column.
	 * 
	 * @param string $v new value
	 * @return void
	 */
	public function setPhone($v)
	{

		if ($this->phone !== $v) {
			$this->phone = $v;
			$this->modifiedColumns[] = CompanyPeer::PHONE;
		}

	} // setPhone()

	/**
	 * Set the value of [fax] column.
	 * 
	 * @param string $v new value
	 * @return void
	 */
	public function setFax($v)
	{

		if ($this->fax !== $v) {
			$this->fax = $v;
			$this->modifiedColumns[] = CompanyPeer::FAX;
		}

	} // setFax()

	/**
	 * Set the value of [url] column.
	 * 
	 * @param string $v new value
	 * @return void
	 */
	public function setUrl($v)
	{

		if ($this->url !== $v) {
			$this->url = $v;
			$this->modifiedColumns[] = CompanyPeer::URL;
		}

	} // setUrl()

	/**
	 * Set the value of [email] column.
	 * 
	 * @param string $v new value
	 * @return void
	 */
	public function setEmail($v)
	{

		if ($this->email !== $v) {
			$this->email = $v;
			$this->modifiedColumns[] = CompanyPeer::EMAIL;
		}

	} // setEmail()

	/**
	 * Set the value of [comments] column.
	 * 
	 * @param string $v new value
	 * @return void
	 */
	public function setComments($v)
	{

		if ($this->comments !== $v) {
			$this->comments = $v;
			$this->modifiedColumns[] = CompanyPeer::COMMENTS;
		}

	} // setComments()

	/**
	 * Set the value of [logo] column.
	 * 
	 * @param string $v new value
	 * @return void
	 */
	public function setLogo($v)
	{

		if ($this->logo !== $v) {
			$this->logo = $v;
			$this->modifiedColumns[] = CompanyPeer::LOGO;
		}

	} // setLogo()

	/**
	 * Set the value of [created_on] column.
	 * 
	 * @param int $v new value
	 * @return void
	 */
	public function setCreatedOn($v)
	{

		if ($v !== null && !is_int($v)) {
			$ts = strtotime($v);
			if ($ts === -1 || $ts === false) { // in PHP 5.1 return value changes to FALSE
				throw new PropelException("Unable to parse date/time value for [created_on] from input: " . var_export($v, true));
			}
		} else {
			$ts = $v;
		}
		if ($this->created_on !== $ts) {
			$this->created_on = $ts;
			$this->modifiedColumns[] = CompanyPeer::CREATED_ON;
		}

	} // setCreatedOn()

	/**
	 * Set the value of [updated_on] column.
	 * 
	 * @param int $v new value
	 * @return void
	 */
	public function setUpdatedOn($v)
	{

		if ($v !== null && !is_int($v)) {
			$ts = strtotime($v);
			if ($ts === -1 || $ts === false) { // in PHP 5.1 return value changes to FALSE
				throw new PropelException("Unable to parse date/time value for [updated_on] from input: " . var_export($v, true));
			}
		} else {
			$ts = $v;
		}
		if ($this->updated_on !== $ts) {
			$this->updated_on = $ts;
			$this->modifiedColumns[] = CompanyPeer::UPDATED_ON;
		}

	} // setUpdatedOn()

	/**
	 * Hydrates (populates) the object variables with values from the database resultset.
	 *
	 * An offset (1-based "start column") is specified so that objects can be hydrated
	 * with a subset of the columns in the resultset rows.  This is needed, for example,
	 * for results of JOIN queries where the resultset row includes columns from two or
	 * more tables.
	 *
	 * @param ResultSet $rs The ResultSet class with cursor advanced to desired record pos.
	 * @param int $startcol 1-based offset column which indicates which restultset column to start with.
	 * @return int next starting column
	 * @throws PropelException  - Any caught Exception will be rewrapped as a PropelException.
	 */
	public function hydrate(ResultSet $rs, $startcol = 1)
	{
		try {

			$this->id = $rs->getInt($startcol + 0);

			$this->name = $rs->getString($startcol + 1);

			$this->address1 = $rs->getString($startcol + 2);

			$this->address2 = $rs->getString($startcol + 3);

			$this->zip_code = $rs->getString($startcol + 4);

			$this->city = $rs->getString($startcol + 5);

			$this->country = $rs->getString($startcol + 6);

			$this->phone = $rs->getString($startcol + 7);

			$this->fax = $rs->getString($startcol + 8);

			$this->url = $rs->getString($startcol + 9);

			$this->email = $rs->getString($startcol + 10);

			$this->comments = $rs->getString($startcol + 11);

			$this->logo = $rs->getString($startcol + 12);

			$this->created_on = $rs->getTimestamp($startcol + 13, null);

			$this->updated_on = $rs->getTimestamp($startcol + 14, null);

			$this->resetModified();

			$this->setNew(false);

			// FIXME - using NUM_COLUMNS may be clearer.
			return $startcol + 15; // 15 = CompanyPeer::NUM_COLUMNS - CompanyPeer::NUM_LAZY_LOAD_COLUMNS).

		} catch (Exception $e) {
			throw new PropelException("Error populating Company object", $e);
		}
	}

	/**
	 * Removes this object from datastore and sets delete attribute.
	 *
	 * @param Connection $con
	 * @return void
	 * @throws PropelException
	 * @see BaseObject::setDeleted()
	 * @see BaseObject::isDeleted()
	 */
	public function delete($con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(CompanyPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			CompanyPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	/**
	 * Stores the object in the database.  If the object is new,
	 * it inserts it; otherwise an update is performed.  This method
	 * wraps the doSave() worker method in a transaction.
	 *
	 * @param Connection $con
	 * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations.
	 * @throws PropelException
	 * @see doSave()
	 */
	public function save($con = null)
	{
    if ($this->isNew() && !$this->isColumnModified('created_on'))
    {
      $this->setCreatedOn(time());
    }

    if ($this->isModified() && !$this->isColumnModified('updated_on'))
    {
      $this->setUpdatedOn(time());
    }

		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(CompanyPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			$affectedRows = $this->doSave($con);
			$con->commit();
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	/**
	 * Stores the object in the database.
	 *
	 * If the object is new, it inserts it; otherwise an update is performed.
	 * All related objects are also updated in this method.
	 *
	 * @param Connection $con
	 * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations.
	 * @throws PropelException
	 * @see save()
	 */
	protected function doSave($con)
	{
		$affectedRows = 0; // initialize var to track total num of affected rows
		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;


			// If this object has been modified, then save it to the database.
			if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = CompanyPeer::doInsert($this, $con);
					$affectedRows += 1; // we are assuming that there is only 1 row per doInsert() which
										 // should always be true here (even though technically
										 // BasePeer::doInsert() can insert multiple rows).

					$this->setId($pk);  //[IMV] update autoincrement primary key

					$this->setNew(false);
				} else {
					$affectedRows += CompanyPeer::doUpdate($this, $con);
				}
				$this->resetModified(); // [HL] After being saved an object is no longer 'modified'
			}

			if ($this->collUsers !== null) {
				foreach($this->collUsers as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			if ($this->collProjects !== null) {
				foreach($this->collProjects as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			if ($this->collWorkorders !== null) {
				foreach($this->collWorkorders as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;
		}
		return $affectedRows;
	} // doSave()

	/**
	 * Array of ValidationFailed objects.
	 * @var array ValidationFailed[]
	 */
	protected $validationFailures = array();

	/**
	 * Gets any ValidationFailed objects that resulted from last call to validate().
	 *
	 *
	 * @return array ValidationFailed[]
	 * @see validate()
	 */
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	/**
	 * Validates the objects modified field values and all objects related to this table.
	 *
	 * If $columns is either a column name or an array of column names
	 * only those columns are validated.
	 *
	 * @param mixed $columns Column name or an array of column names.
	 * @return boolean Whether all columns pass validation.
	 * @see doValidate()
	 * @see getValidationFailures()
	 */
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	/**
	 * This function performs the validation work for complex object models.
	 *
	 * In addition to checking the current object, all related objects will
	 * also be validated.  If all pass then <code>true</code> is returned; otherwise
	 * an aggreagated array of ValidationFailed objects will be returned.
	 *
	 * @param array $columns Array of column names to validate.
	 * @return mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise.
	 */
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


			if (($retval = CompanyPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collUsers !== null) {
					foreach($this->collUsers as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}

				if ($this->collProjects !== null) {
					foreach($this->collProjects as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}

				if ($this->collWorkorders !== null) {
					foreach($this->collWorkorders as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	/**
	 * Retrieves a field from the object by name passed in as a string.
	 *
	 * @param string $name name
	 * @param string $type The type of fieldname the $name is of:
	 *                     one of the class type constants TYPE_PHPNAME,
	 *                     TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM
	 * @return mixed Value of field.
	 */
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = CompanyPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	/**
	 * Retrieves a field from the object by Position as specified in the xml schema.
	 * Zero-based.
	 *
	 * @param int $pos position in xml schema
	 * @return mixed Value of field at $pos
	 */
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getName();
				break;
			case 2:
				return $this->getAddress1();
				break;
			case 3:
				return $this->getAddress2();
				break;
			case 4:
				return $this->getZipCode();
				break;
			case 5:
				return $this->getCity();
				break;
			case 6:
				return $this->getCountry();
				break;
			case 7:
				return $this->getPhone();
				break;
			case 8:
				return $this->getFax();
				break;
			case 9:
				return $this->getUrl();
				break;
			case 10:
				return $this->getEmail();
				break;
			case 11:
				return $this->getComments();
				break;
			case 12:
				return $this->getLogo();
				break;
			case 13:
				return $this->getCreatedOn();
				break;
			case 14:
				return $this->getUpdatedOn();
				break;
			default:
				return null;
				break;
		} // switch()
	}

	/**
	 * Exports the object as an array.
	 *
	 * You can specify the key type of the array by passing one of the class
	 * type constants.
	 *
	 * @param string $keyType One of the class type constants TYPE_PHPNAME,
	 *                        TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM
	 * @return an associative array containing the field names (as keys) and field values
	 */
	public function toArray($keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = CompanyPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getName(),
			$keys[2] => $this->getAddress1(),
			$keys[3] => $this->getAddress2(),
			$keys[4] => $this->getZipCode(),
			$keys[5] => $this->getCity(),
			$keys[6] => $this->getCountry(),
			$keys[7] => $this->getPhone(),
			$keys[8] => $this->getFax(),
			$keys[9] => $this->getUrl(),
			$keys[10] => $this->getEmail(),
			$keys[11] => $this->getComments(),
			$keys[12] => $this->getLogo(),
			$keys[13] => $this->getCreatedOn(),
			$keys[14] => $this->getUpdatedOn(),
		);
		return $result;
	}

	/**
	 * Sets a field from the object by name passed in as a string.
	 *
	 * @param string $name peer name
	 * @param mixed $value field value
	 * @param string $type The type of fieldname the $name is of:
	 *                     one of the class type constants TYPE_PHPNAME,
	 *                     TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM
	 * @return void
	 */
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = CompanyPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	/**
	 * Sets a field from the object by Position as specified in the xml schema.
	 * Zero-based.
	 *
	 * @param int $pos position in xml schema
	 * @param mixed $value field value
	 * @return void
	 */
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setName($value);
				break;
			case 2:
				$this->setAddress1($value);
				break;
			case 3:
				$this->setAddress2($value);
				break;
			case 4:
				$this->setZipCode($value);
				break;
			case 5:
				$this->setCity($value);
				break;
			case 6:
				$this->setCountry($value);
				break;
			case 7:
				$this->setPhone($value);
				break;
			case 8:
				$this->setFax($value);
				break;
			case 9:
				$this->setUrl($value);
				break;
			case 10:
				$this->setEmail($value);
				break;
			case 11:
				$this->setComments($value);
				break;
			case 12:
				$this->setLogo($value);
				break;
			case 13:
				$this->setCreatedOn($value);
				break;
			case 14:
				$this->setUpdatedOn($value);
				break;
		} // switch()
	}

	/**
	 * Populates the object using an array.
	 *
	 * This is particularly useful when populating an object from one of the
	 * request arrays (e.g. $_POST).  This method goes through the column
	 * names, checking to see whether a matching key exists in populated
	 * array. If so the setByName() method is called for that column.
	 *
	 * You can specify the key type of the array by additionally passing one
	 * of the class type constants TYPE_PHPNAME, TYPE_COLNAME, TYPE_FIELDNAME,
	 * TYPE_NUM. The default key type is the column's phpname (e.g. 'authorId')
	 *
	 * @param array  $arr     An array to populate the object from.
	 * @param string $keyType The type of keys the array uses.
	 * @return void
	 */
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = CompanyPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setName($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setAddress1($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setAddress2($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setZipCode($arr[$keys[4]]);
		if (array_key_exists($keys[5], $arr)) $this->setCity($arr[$keys[5]]);
		if (array_key_exists($keys[6], $arr)) $this->setCountry($arr[$keys[6]]);
		if (array_key_exists($keys[7], $arr)) $this->setPhone($arr[$keys[7]]);
		if (array_key_exists($keys[8], $arr)) $this->setFax($arr[$keys[8]]);
		if (array_key_exists($keys[9], $arr)) $this->setUrl($arr[$keys[9]]);
		if (array_key_exists($keys[10], $arr)) $this->setEmail($arr[$keys[10]]);
		if (array_key_exists($keys[11], $arr)) $this->setComments($arr[$keys[11]]);
		if (array_key_exists($keys[12], $arr)) $this->setLogo($arr[$keys[12]]);
		if (array_key_exists($keys[13], $arr)) $this->setCreatedOn($arr[$keys[13]]);
		if (array_key_exists($keys[14], $arr)) $this->setUpdatedOn($arr[$keys[14]]);
	}

	/**
	 * Build a Criteria object containing the values of all modified columns in this object.
	 *
	 * @return Criteria The Criteria object containing all modified values.
	 */
	public function buildCriteria()
	{
		$criteria = new Criteria(CompanyPeer::DATABASE_NAME);

		if ($this->isColumnModified(CompanyPeer::ID)) $criteria->add(CompanyPeer::ID, $this->id);
		if ($this->isColumnModified(CompanyPeer::NAME)) $criteria->add(CompanyPeer::NAME, $this->name);
		if ($this->isColumnModified(CompanyPeer::ADDRESS1)) $criteria->add(CompanyPeer::ADDRESS1, $this->address1);
		if ($this->isColumnModified(CompanyPeer::ADDRESS2)) $criteria->add(CompanyPeer::ADDRESS2, $this->address2);
		if ($this->isColumnModified(CompanyPeer::ZIP_CODE)) $criteria->add(CompanyPeer::ZIP_CODE, $this->zip_code);
		if ($this->isColumnModified(CompanyPeer::CITY)) $criteria->add(CompanyPeer::CITY, $this->city);
		if ($this->isColumnModified(CompanyPeer::COUNTRY)) $criteria->add(CompanyPeer::COUNTRY, $this->country);
		if ($this->isColumnModified(CompanyPeer::PHONE)) $criteria->add(CompanyPeer::PHONE, $this->phone);
		if ($this->isColumnModified(CompanyPeer::FAX)) $criteria->add(CompanyPeer::FAX, $this->fax);
		if ($this->isColumnModified(CompanyPeer::URL)) $criteria->add(CompanyPeer::URL, $this->url);
		if ($this->isColumnModified(CompanyPeer::EMAIL)) $criteria->add(CompanyPeer::EMAIL, $this->email);
		if ($this->isColumnModified(CompanyPeer::COMMENTS)) $criteria->add(CompanyPeer::COMMENTS, $this->comments);
		if ($this->isColumnModified(CompanyPeer::LOGO)) $criteria->add(CompanyPeer::LOGO, $this->logo);
		if ($this->isColumnModified(CompanyPeer::CREATED_ON)) $criteria->add(CompanyPeer::CREATED_ON, $this->created_on);
		if ($this->isColumnModified(CompanyPeer::UPDATED_ON)) $criteria->add(CompanyPeer::UPDATED_ON, $this->updated_on);

		return $criteria;
	}

	/**
	 * Builds a Criteria object containing the primary key for this object.
	 *
	 * Unlike buildCriteria() this method includes the primary key values regardless
	 * of whether or not they have been modified.
	 *
	 * @return Criteria The Criteria object containing value(s) for primary key(s).
	 */
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(CompanyPeer::DATABASE_NAME);

		$criteria->add(CompanyPeer::ID, $this->id);

		return $criteria;
	}

	/**
	 * Returns the primary key for this object (row).
	 * @return int
	 */
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	/**
	 * Generic method to set the primary key (id column).
	 *
	 * @param int $key Primary key.
	 * @return void
	 */
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	/**
	 * Sets contents of passed object to values from current object.
	 *
	 * If desired, this method can also make copies of all associated (fkey referrers)
	 * objects.
	 *
	 * @param object $copyObj An object of Company (or compatible) type.
	 * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
	 * @throws PropelException
	 */
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setName($this->name);

		$copyObj->setAddress1($this->address1);

		$copyObj->setAddress2($this->address2);

		$copyObj->setZipCode($this->zip_code);

		$copyObj->setCity($this->city);

		$copyObj->setCountry($this->country);

		$copyObj->setPhone($this->phone);

		$copyObj->setFax($this->fax);

		$copyObj->setUrl($this->url);

		$copyObj->setEmail($this->email);

		$copyObj->setComments($this->comments);

		$copyObj->setLogo($this->logo);

		$copyObj->setCreatedOn($this->created_on);

		$copyObj->setUpdatedOn($this->updated_on);


		if ($deepCopy) {
			// important: temporarily setNew(false) because this affects the behavior of
			// the getter/setter methods for fkey referrer objects.
			$copyObj->setNew(false);

			foreach($this->getUsers() as $relObj) {
				$copyObj->addUser($relObj->copy($deepCopy));
			}

			foreach($this->getProjects() as $relObj) {
				$copyObj->addProject($relObj->copy($deepCopy));
			}

			foreach($this->getWorkorders() as $relObj) {
				$copyObj->addWorkorder($relObj->copy($deepCopy));
			}

		} // if ($deepCopy)


		$copyObj->setNew(true);

		$copyObj->setId(NULL); // this is a pkey column, so set to default value

	}

	/**
	 * Makes a copy of this object that will be inserted as a new row in table when saved.
	 * It creates a new object filling in the simple attributes, but skipping any primary
	 * keys that are defined for the table.
	 *
	 * If desired, this method can also make copies of all associated (fkey referrers)
	 * objects.
	 *
	 * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
	 * @return Company Clone of current object.
	 * @throws PropelException
	 */
	public function copy($deepCopy = false)
	{
		// we use get_class(), because this might be a subclass
		$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	/**
	 * Returns a peer instance associated with this om.
	 *
	 * Since Peer classes are not to have any instance attributes, this method returns the
	 * same instance for all member of this class. The method could therefore
	 * be static, but this would prevent one from overriding the behavior.
	 *
	 * @return CompanyPeer
	 */
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new CompanyPeer();
		}
		return self::$peer;
	}

	/**
	 * Temporary storage of collUsers to save a possible db hit in
	 * the event objects are add to the collection, but the
	 * complete collection is never requested.
	 * @return void
	 */
	public function initUsers()
	{
		if ($this->collUsers === null) {
			$this->collUsers = array();
		}
	}

	/**
	 * If this collection has already been initialized with
	 * an identical criteria, it returns the collection.
	 * Otherwise if this Company has previously
	 * been saved, it will retrieve related Users from storage.
	 * If this Company is new, it will return
	 * an empty collection or the current collection, the criteria
	 * is ignored on a new object.
	 *
	 * @param Connection $con
	 * @param Criteria $criteria
	 * @throws PropelException
	 */
	public function getUsers($criteria = null, $con = null)
	{
		// include the Peer class
		include_once 'model/om/BaseUserPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collUsers === null) {
			if ($this->isNew()) {
			   $this->collUsers = array();
			} else {

				$criteria->add(UserPeer::COMPANY_ID, $this->getId());

				UserPeer::addSelectColumns($criteria);
				$this->collUsers = UserPeer::doSelect($criteria, $con);
			}
		} else {
			// criteria has no effect for a new object
			if (!$this->isNew()) {
				// the following code is to determine if a new query is
				// called for.  If the criteria is the same as the last
				// one, just return the collection.


				$criteria->add(UserPeer::COMPANY_ID, $this->getId());

				UserPeer::addSelectColumns($criteria);
				if (!isset($this->lastUserCriteria) || !$this->lastUserCriteria->equals($criteria)) {
					$this->collUsers = UserPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastUserCriteria = $criteria;
		return $this->collUsers;
	}

	/**
	 * Returns the number of related Users.
	 *
	 * @param Criteria $criteria
	 * @param boolean $distinct
	 * @param Connection $con
	 * @throws PropelException
	 */
	public function countUsers($criteria = null, $distinct = false, $con = null)
	{
		// include the Peer class
		include_once 'model/om/BaseUserPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(UserPeer::COMPANY_ID, $this->getId());

		return UserPeer::doCount($criteria, $distinct, $con);
	}

	/**
	 * Method called to associate a User object to this object
	 * through the User foreign key attribute
	 *
	 * @param User $l User
	 * @return void
	 * @throws PropelException
	 */
	public function addUser(User $l)
	{
		$this->collUsers[] = $l;
		$l->setCompany($this);
	}

	/**
	 * Temporary storage of collProjects to save a possible db hit in
	 * the event objects are add to the collection, but the
	 * complete collection is never requested.
	 * @return void
	 */
	public function initProjects()
	{
		if ($this->collProjects === null) {
			$this->collProjects = array();
		}
	}

	/**
	 * If this collection has already been initialized with
	 * an identical criteria, it returns the collection.
	 * Otherwise if this Company has previously
	 * been saved, it will retrieve related Projects from storage.
	 * If this Company is new, it will return
	 * an empty collection or the current collection, the criteria
	 * is ignored on a new object.
	 *
	 * @param Connection $con
	 * @param Criteria $criteria
	 * @throws PropelException
	 */
	public function getProjects($criteria = null, $con = null)
	{
		// include the Peer class
		include_once 'model/om/BaseProjectPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collProjects === null) {
			if ($this->isNew()) {
			   $this->collProjects = array();
			} else {

				$criteria->add(ProjectPeer::COMPANY_ID, $this->getId());

				ProjectPeer::addSelectColumns($criteria);
				$this->collProjects = ProjectPeer::doSelect($criteria, $con);
			}
		} else {
			// criteria has no effect for a new object
			if (!$this->isNew()) {
				// the following code is to determine if a new query is
				// called for.  If the criteria is the same as the last
				// one, just return the collection.


				$criteria->add(ProjectPeer::COMPANY_ID, $this->getId());

				ProjectPeer::addSelectColumns($criteria);
				if (!isset($this->lastProjectCriteria) || !$this->lastProjectCriteria->equals($criteria)) {
					$this->collProjects = ProjectPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastProjectCriteria = $criteria;
		return $this->collProjects;
	}

	/**
	 * Returns the number of related Projects.
	 *
	 * @param Criteria $criteria
	 * @param boolean $distinct
	 * @param Connection $con
	 * @throws PropelException
	 */
	public function countProjects($criteria = null, $distinct = false, $con = null)
	{
		// include the Peer class
		include_once 'model/om/BaseProjectPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(ProjectPeer::COMPANY_ID, $this->getId());

		return ProjectPeer::doCount($criteria, $distinct, $con);
	}

	/**
	 * Method called to associate a Project object to this object
	 * through the Project foreign key attribute
	 *
	 * @param Project $l Project
	 * @return void
	 * @throws PropelException
	 */
	public function addProject(Project $l)
	{
		$this->collProjects[] = $l;
		$l->setCompany($this);
	}


	/**
	 * If this collection has already been initialized with
	 * an identical criteria, it returns the collection.
	 * Otherwise if this Company is new, it will return
	 * an empty collection; or if this Company has previously
	 * been saved, it will retrieve related Projects from storage.
	 *
	 * This method is protected by default in order to keep the public
	 * api reasonable.  You can provide public methods for those you
	 * actually need in Company.
	 */
	public function getProjectsJoinUser($criteria = null, $con = null)
	{
		// include the Peer class
		include_once 'model/om/BaseProjectPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collProjects === null) {
			if ($this->isNew()) {
				$this->collProjects = array();
			} else {

				$criteria->add(ProjectPeer::COMPANY_ID, $this->getId());

				$this->collProjects = ProjectPeer::doSelectJoinUser($criteria, $con);
			}
		} else {
			// the following code is to determine if a new query is
			// called for.  If the criteria is the same as the last
			// one, just return the collection.

			$criteria->add(ProjectPeer::COMPANY_ID, $this->getId());

			if (!isset($this->lastProjectCriteria) || !$this->lastProjectCriteria->equals($criteria)) {
				$this->collProjects = ProjectPeer::doSelectJoinUser($criteria, $con);
			}
		}
		$this->lastProjectCriteria = $criteria;

		return $this->collProjects;
	}

	/**
	 * Temporary storage of collWorkorders to save a possible db hit in
	 * the event objects are add to the collection, but the
	 * complete collection is never requested.
	 * @return void
	 */
	public function initWorkorders()
	{
		if ($this->collWorkorders === null) {
			$this->collWorkorders = array();
		}
	}

	/**
	 * If this collection has already been initialized with
	 * an identical criteria, it returns the collection.
	 * Otherwise if this Company has previously
	 * been saved, it will retrieve related Workorders from storage.
	 * If this Company is new, it will return
	 * an empty collection or the current collection, the criteria
	 * is ignored on a new object.
	 *
	 * @param Connection $con
	 * @param Criteria $criteria
	 * @throws PropelException
	 */
	public function getWorkorders($criteria = null, $con = null)
	{
		// include the Peer class
		include_once 'model/om/BaseWorkorderPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collWorkorders === null) {
			if ($this->isNew()) {
			   $this->collWorkorders = array();
			} else {

				$criteria->add(WorkorderPeer::COMPANY_ID, $this->getId());

				WorkorderPeer::addSelectColumns($criteria);
				$this->collWorkorders = WorkorderPeer::doSelect($criteria, $con);
			}
		} else {
			// criteria has no effect for a new object
			if (!$this->isNew()) {
				// the following code is to determine if a new query is
				// called for.  If the criteria is the same as the last
				// one, just return the collection.


				$criteria->add(WorkorderPeer::COMPANY_ID, $this->getId());

				WorkorderPeer::addSelectColumns($criteria);
				if (!isset($this->lastWorkorderCriteria) || !$this->lastWorkorderCriteria->equals($criteria)) {
					$this->collWorkorders = WorkorderPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastWorkorderCriteria = $criteria;
		return $this->collWorkorders;
	}

	/**
	 * Returns the number of related Workorders.
	 *
	 * @param Criteria $criteria
	 * @param boolean $distinct
	 * @param Connection $con
	 * @throws PropelException
	 */
	public function countWorkorders($criteria = null, $distinct = false, $con = null)
	{
		// include the Peer class
		include_once 'model/om/BaseWorkorderPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(WorkorderPeer::COMPANY_ID, $this->getId());

		return WorkorderPeer::doCount($criteria, $distinct, $con);
	}

	/**
	 * Method called to associate a Workorder object to this object
	 * through the Workorder foreign key attribute
	 *
	 * @param Workorder $l Workorder
	 * @return void
	 * @throws PropelException
	 */
	public function addWorkorder(Workorder $l)
	{
		$this->collWorkorders[] = $l;
		$l->setCompany($this);
	}


	/**
	 * If this collection has already been initialized with
	 * an identical criteria, it returns the collection.
	 * Otherwise if this Company is new, it will return
	 * an empty collection; or if this Company has previously
	 * been saved, it will retrieve related Workorders from storage.
	 *
	 * This method is protected by default in order to keep the public
	 * api reasonable.  You can provide public methods for those you
	 * actually need in Company.
	 */
	public function getWorkordersJoinUserRelatedByPmId($criteria = null, $con = null)
	{
		// include the Peer class
		include_once 'model/om/BaseWorkorderPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collWorkorders === null) {
			if ($this->isNew()) {
				$this->collWorkorders = array();
			} else {

				$criteria->add(WorkorderPeer::COMPANY_ID, $this->getId());

				$this->collWorkorders = WorkorderPeer::doSelectJoinUserRelatedByPmId($criteria, $con);
			}
		} else {
			// the following code is to determine if a new query is
			// called for.  If the criteria is the same as the last
			// one, just return the collection.

			$criteria->add(WorkorderPeer::COMPANY_ID, $this->getId());

			if (!isset($this->lastWorkorderCriteria) || !$this->lastWorkorderCriteria->equals($criteria)) {
				$this->collWorkorders = WorkorderPeer::doSelectJoinUserRelatedByPmId($criteria, $con);
			}
		}
		$this->lastWorkorderCriteria = $criteria;

		return $this->collWorkorders;
	}


	/**
	 * If this collection has already been initialized with
	 * an identical criteria, it returns the collection.
	 * Otherwise if this Company is new, it will return
	 * an empty collection; or if this Company has previously
	 * been saved, it will retrieve related Workorders from storage.
	 *
	 * This method is protected by default in order to keep the public
	 * api reasonable.  You can provide public methods for those you
	 * actually need in Company.
	 */
	public function getWorkordersJoinUserRelatedByTechId($criteria = null, $con = null)
	{
		// include the Peer class
		include_once 'model/om/BaseWorkorderPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collWorkorders === null) {
			if ($this->isNew()) {
				$this->collWorkorders = array();
			} else {

				$criteria->add(WorkorderPeer::COMPANY_ID, $this->getId());

				$this->collWorkorders = WorkorderPeer::doSelectJoinUserRelatedByTechId($criteria, $con);
			}
		} else {
			// the following code is to determine if a new query is
			// called for.  If the criteria is the same as the last
			// one, just return the collection.

			$criteria->add(WorkorderPeer::COMPANY_ID, $this->getId());

			if (!isset($this->lastWorkorderCriteria) || !$this->lastWorkorderCriteria->equals($criteria)) {
				$this->collWorkorders = WorkorderPeer::doSelectJoinUserRelatedByTechId($criteria, $con);
			}
		}
		$this->lastWorkorderCriteria = $criteria;

		return $this->collWorkorders;
	}

} // BaseCompany
